var currPage = 1;
// 每页默认12条数据
var pageSize = 5;
// 获取传递的参数  /showhero.html?currPage=2--?currPage=2
var search = location.search
console.log(search)
// 按照=对参数分割
var arr = search.split("=")
// 第一次访问页面时，是没有传递参数的，只有在分页时才会传递参数
// 防止第一次出现问题
/*console.log(arr.length)*/
if(arr.length >1){
    currPage = arr[1]
}
var  c
console.log(currPage)
var b = currPage.split("&")
if(b.length >1){
    c = b[0]
}
console.log(c)
/*if(currPage <10){
    c = currPage.substring(0,1)
}else {
    c = currPage.substring(0,2)
}*/
console.log(c)

var d;
if(c>0 && c<100 ){
    d =c;
}else {
    d=1;

}
function getCookie(name) {
    var prefix = name + "="  // name=
    console.log(document.cookie) // name=wangwu; Hm_lvt_eaa57ca47dacb4ad4f5a2570
    // name=  第一次出现的位置
    var start = document.cookie.indexOf(prefix)
    if (start == -1) {
        return null;
    }
    // 从0+5这个位置开始，找到;第一次出现的位置，也就是这个cookie值得结束位置
    var end = document.cookie.indexOf(";", start + prefix.length)
    // name=wangwu
    // Hm_lvt_eaa57ca47dacb4ad4f5a2570;name=wangwu
    // 将字符串得总长度就是截取值得结束位置
    if (end == -1) { // 没有找到分号
        end = document.cookie.length;
    }
    var value = document.cookie.substring(start + prefix.length, end)
    return unescape(value);
}
    var Id = getCookie("id")

   $.getJSON("http://localhost:8080/selectbyuserId?cookieId=" + Id + "&currPage=" + d + "&pageSize=" + pageSize , function (data) {
       var list = data.data.list
       f(list)
       generatePage(data.data.navigatepageNums, data.data.pageNum, data.data.pages)
   });

   function f(list){
       let html = ''
       for (let i=0;i < list.length; i++){
           html += `<tr>
             <td><a href="http://localhost:63343/qiguai/html/single-goods.html?id=${list[i].goodId}">${list[i].collectId}</a></td>
             <td><a href="http://localhost:63343/qiguai/html/single-goods.html?id=${list[i].goodId}"><img src="${list[i].goodPhoto}" style="width: 148px;height: 148px"></a></td>
             <td>${list[i].goodBrand}</td>
             <td style="height: 50px;">${list[i].goodName}</td>
             <td>${list[i].goodUpper}</td>
             <td>￥${list[i].goodMoney}</td>
             <td><button data-collectid="${list[i].collectId}" data-goodname="${list[i].goodName}" data-toggle="modal" data-target="#myModal"
            class="btn btn-primary">删除</button></td>
             </tr> `
       }
       $("table").append(html)
   }

   function generatePage(navigatepageNums, pageNum, pages){
     var html = ''
       if (pageNum == 1){
           html += `<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>`
       } else {
           html += `<li><a href="http://localhost:63343/qiguai/html/CollectShow.html?currPage=${pageNum-1}&pageSize=5" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>`
       }
       for (let i = 0;i < navigatepageNums.length; i++){
           if (pageNum == navigatepageNums[i]){
               html +=`<li class="active"><a href="#">${navigatepageNums[i]}</a></li>`
           } else {
               html += `<li><a href="http://localhost:63343/qiguai/html/CollectShow.html?currPage=${navigatepageNums[i]}&pageSize=5">${navigatepageNums[i]}</a></li>`
           }
       }
       if (pageNum == pages){
           html += `<li class="disabled"><a href="#" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>`
       }else {
           html += `<li><a href="http://localhost:63343/qiguai/html/CollectShow.html?currPage=${pageNum+1}&pageSize=5" onclick="" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>`
       }
       $("#ul").append(html)
   }

    /*$.ajax({
    url:"http://localhost:8080/collectAll",
    type:"get",
    dataType:"json",
    success:function (data){
    console.log(data)
    let html = ''
    $.each(data.data,function (index, value){
    html += `<tr>
             <td>${value.collectId}</td>
             <td><img src="${value.goodPhoto}" style="width: 148px;height: 148px"></td>
             <td>${value.goodBrand}</td>
             <td style="height: 50px;">${value.goodName}</td>
             <td>${value.goodUpper}</td>
             <td>￥${value.goodMoney}</td>
             <td><button data-collectid="${value.collectId}" data-goodname="${value.goodName}" data-toggle="modal" data-target="#myModal"
            class="btn btn-primary">删除</button></td>
             </tr>`
})
    $("table").append(html)
}
})*/


// $("button").click(function (){
//     var text = $(this).text() // 获取按钮之间的文本内容
    // if(text.trim()=='删除'){
        // var heroname = $(this).attr("data")
        // if(confirm("确定要删除 " + heroname + " 吗?")){// 点击确定confirm就会返回true
        //     // 开始执行删除
        //     var id = $(this).attr("value")
        //     console.log("id=" + id)
        //     $.getJSON("/web03/lolhero","action=deletebyid&id=" + id,function (data){
        //         console.log("data=" + data)
        //         if(data.code==1){
        //             window.location.href = "/web03/lolhero/showhero.html"
        //         } else if(data.code==0){
        //             alert("删除失败")
        //         }
        //     })
        // }} else
//     if(text.trim()=='修改'){
//         let id = $(this).attr("value")
//         location.href="/project/shoe/update.html?id=" + id;
//     }
// })


var goodId;
$('#myModal').on('show.bs.modal', function (event) {
    // 触发模态框的按钮
    var button = $(event.relatedTarget) // Button that triggered the modal
    // 从data-*内容中获取数据
    var goodName = button.data('goodname') // Extract info from data-* attributes
    goodId = button.data('collectid') // Extract info from data-* attributes
    // 获取到modal 模态框对象
    var modal = $(this)
    modal.find('.modal-title').text('删除功能')
    modal.find('.modal-body p span').html(goodName)
})

$("#myModal .modal-footer button").eq(1).click(function (){
    $.getJSON("http://localhost:8080/deletebyid?collectid=" + goodId, data => {
                if(data==1){
                    console.log("data=" + data);
                    alert("删除成功");
                    location.reload();
                } else {
                    alert("删除失败")
                }
            })
    $('#myModal').modal('toggle')
})